//
//  WH_ClassificationController.m
//  App_6.23
//
//  Created by wanghao on 16/6/28.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_ClassificationController.h"
#import "WH_Classification.h"
#import "WH_ClassListVC.h"
@interface WH_ClassificationController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *imageArr;

@end

@implementation WH_ClassificationController

-(void)viewWillAppear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self createTab];
    [self createHub];
    [self hubShow];
    [self postData];
}

-(void)createTab{
    
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    self.myTableView.rowHeight = SCREEN_HEIGHT/11;
    self.myTableView.sectionHeaderHeight = 40;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tabCell"];
    self.myTableView.scrollEnabled = NO;
    [self.myTableView setHidden:YES];
}
#pragma mark - postData
-(void)postData{
    NSString *body = @"action=category";
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        NSDictionary *dic = result;
        NSArray *category_array = [dic objectForKey:@"category_array"];
        NSMutableArray *tempArr = [NSMutableArray array];
        self.dataArr = [NSMutableArray array];
        for (NSDictionary *dic in category_array) {
            
            WH_Classification *classtion = [WH_Classification modelWithDictionary:dic];
            [tempArr addObject:classtion];
        }
        
        for (int i = 0; i < tempArr.count; i++) {
            [self.dataArr addObject:tempArr[tempArr.count-1-i]];
        }
        [self.myTableView reloadData];
        [self hubDismiss];
        [self.myTableView setHidden:NO];
    } failure:^(NSError *error) {
        
        
    }];
    
}

#pragma mark - Tab&Delegate&DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *tabCell = @"tabCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tabCell];
    WH_Classification *classModel = self.dataArr[indexPath.row];
    
    cell.textLabel.text = classModel.category_name;
    cell.textLabel.font = [UIFont systemFontOfSize:15.f];
    cell.textLabel.alpha = .7;
    cell.imageView.image = [UIImage imageNamed:classModel.category_image];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    label.text = @"    分类浏览";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:13];
    UILabel *labelLine =  [[UILabel alloc] initWithFrame:CGRectMake(10, label.frame.size.height, SCREEN_WIDTH, 1)];
    labelLine.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
    [label addSubview:labelLine];
    return label;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WH_ClassListVC *listVC = [[WH_ClassListVC alloc] init];
    WH_Classification *list = self.dataArr[indexPath.row];
    listVC.categoryID = list.category_id;
    [self.navigationController pushViewController:listVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
